package com.jg.dao.impl;

import com.jg.dao.BaseDao;
import com.jg.dao.EmployeeProfilesDao;
import com.jg.entity.EmployeeProfiles;
import com.jg.util.Page;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 86130
 * @version 1.0
 * @description: TODO
 * @date 2025/3/27 10:31
 */
public class EmployeeProfilesDaoImpl extends BaseDao implements EmployeeProfilesDao {

    @Override
    public Page<EmployeeProfiles> findEmployeeProfilesAll(Page<EmployeeProfiles> page) {
        List<Object> params = new ArrayList<>();
        List<EmployeeProfiles> list = new ArrayList<>();
        String sql = "select * from employee_profiles";
        params.add((page.getCurrentPage() - 1) * page.getPageSize());
        params.add(page.getPageSize());
        ResultSet rs = executeQuery(sql,params.toArray());
        if (rs != null) {
            try {
                while (rs.next()) {
                    EmployeeProfiles employeeProfiles = new EmployeeProfiles();
                    employeeProfiles.setId(rs.getInt("id"));
                    employeeProfiles.setUserId(rs.getInt("user_id"));
                    employeeProfiles.setName(rs.getString("name"));
                    employeeProfiles.setPosition(rs.getString("position"));
                    employeeProfiles.setDepartment(rs.getString("department"));
                    employeeProfiles.setHireDate(rs.getDate("hire_date"));
                    employeeProfiles.setLeaveDate(rs.getDate("leave_date"));
                    employeeProfiles.setStatus(rs.getString("status"));
                    list.add(employeeProfiles);
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                closedAll(rs);
            }
        }
        page.setData(list);
        page.setTotalCount(totalCount());

        return page;
    }

    private int totalCount() {
        int totalCount = 0;
        List<Object> params = new ArrayList<>();
        String sql = "select count(*) count from employee_profiles";
        ResultSet rs = executeQuery(sql, params.toArray());
        if (null != rs) {
            try {
                while (rs.next()) {
                    totalCount = rs.getInt("count");
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                closedAll(rs);
            }
        }
        return totalCount;
    }

    @Override
    public EmployeeProfiles findEmployeeProfilesById(Integer id) {
        String sql = "select * from employee_profiles where id = ?";
        ResultSet rs = executeQuery(sql, id);
        EmployeeProfiles employeeProfiles = null;
        if (rs != null) {
            try {
                while (rs.next()) {
                    employeeProfiles = new EmployeeProfiles();
                    employeeProfiles.setId(rs.getInt("id"));
                    employeeProfiles.setUserId(rs.getInt("user_id"));
                    employeeProfiles.setName(rs.getString("name"));
                    employeeProfiles.setPosition(rs.getString("position"));
                    employeeProfiles.setDepartment(rs.getString("department"));
                    employeeProfiles.setHireDate(rs.getDate("hire_date"));
                    employeeProfiles.setLeaveDate(rs.getDate("leave_date"));
                    employeeProfiles.setStatus(rs.getString("status"));
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                closedAll(rs);
            }
        }
        return employeeProfiles;
    }
}
